package com.bawei.common.utils;

import java.io.*;
import java.nio.charset.Charset;
import java.util.InputMismatchException;
import java.util.Scanner;

/**
 * Created by mi on 2021/4/25
 */
public class StreamUtil {
    /**
     *
     * @Title: closeStream
     * @Description: 关闭多个io流
     * @param closeables
     * @return void
     */
    public static void closeStream(Closeable... closeables) {
        try {
            for (Closeable c : closeables) {
                c.close();
            }
        } catch (IOException e) {

        }
    }
    /**
     * 拷贝流
     * @param src
     * @param out
     * @param isCloseInputStream
     * @param isCloseOutputStream
     * @throws IOException
     */
    public static void copy(InputStream src, OutputStream out, boolean isCloseInputStream, boolean isCloseOutputStream)  throws IOException {
            //文件拷贝
            byte flush[]  = new byte[1024];
            int len = 0;
            while(0<=(len=src.read(flush))){
                out.write(flush, 0, len);
            }
            //关闭流的注意 先打开的后关
            out.close();
            src.close();
    }

    public static void readTextFile(InputStream src,String path) throws IOException{
        File file;
        OutputStream out = new FileOutputStream(new File(path));
        InputStreamReader isr = new InputStreamReader(src, "utf-8");
        copy(src,out, true, true);
    }
    public static String readStringFromSystemIn(String message){
        String str="";
        try {
            BufferedReader e = new BufferedReader(new InputStreamReader(
                    new ByteArrayInputStream(message.getBytes(Charset
                            .forName("utf8"))), Charset.forName("utf8")));
            while ((str = e.readLine()) != null) {
                System.out.println("读取控制台内容："+str);
            }
            e.close();
        } catch (IOException arg) {
            arg.printStackTrace();
        }
        return str;
    }

    public static int readIntFromSystemIn(String message){
        String str=null;
        int i=0;
        try {
            BufferedReader e = new BufferedReader(new InputStreamReader(
                    new ByteArrayInputStream(message.getBytes(Charset
                            .forName("utf8"))), Charset.forName("utf8")));
            while ((str = e.readLine()) != null) {
                i =Integer.valueOf(str);
                System.out.println("读取控制台内容："+i);
            }
            e.close();
        } catch (IOException arg) {
            arg.printStackTrace();
        }
        return i;
    }

    public static void main(String[] args) throws IOException {
        Scanner input = new Scanner(System.in);
        int num = 0;
        while (num == 0) {
            try {
                System.out.println("请输入年龄：");
                num = input.nextInt();
                StreamUtil.readIntFromSystemIn(num+"");
                System.out.println("您的年龄为："+num);
            } catch (InputMismatchException e) {
                System.out.println("必须输入整数！请重新输入：");
                input.nextLine(); // 吃掉回车
            }
        }
//       System.out.println("请输出您的姓名：");
//        String str = input.nextLine();
//        StreamUtil.readStringFromSystemIn(str);
/*        File file = new File("E:\\1901A\\exam\\test.txt");
        File file1 = new File("E:\\1901A\\exam\\test1.txt");
        InputStream in = new FileInputStream(file);
        OutputStream out = new FileOutputStream(file1);
//        StreamUtil.copy(in, out,true , true);
        StreamUtil.readTextFile(in,"E:\\1901A\\exam\\test1.txt");*/
    }
}
